package com.chat.view;

import com.chat.Local;
import com.chat.common.User;
import com.chat.service.CheckEmail;
import com.chat.service.CheckSecurityCode;
import com.chat.service.EmailService;
import com.chat.utils.IsMail;
import com.chat.utils.MouseFollow;
import com.chat.utils.SecurityCode2;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.apache.commons.lang.RandomStringUtils;

public class ForgetController {
    public Text yanzheng;
    @FXML
    private Button returnbutton;
    @FXML
    private Button sendbutton;
    @FXML
    protected TextField mailboxTextField;
    @FXML
    private TextField securitycodeTextField;
    @FXML
    private Label mailboxlabel;
    @FXML
    private Button confirmbutton;
    @FXML
    private Label confirmlabel;
    public String content;//存储验证码
    public void initialize(){
          yanzheng.setText("点击发送验证码");
    }
    public void returnbuttonOnAction(ActionEvent event)
    {
        Stage stage=(Stage) returnbutton.getScene().getWindow();
        stage.close();
        try {
            FXMLLoader fxmlLoader = new FXMLLoader();
            fxmlLoader.setLocation(getClass().getResource("login.fxml"));
            Parent root = fxmlLoader.load();
            Stage loginstage=new Stage();
            Scene scene = new Scene(root, 600, 400);
            loginstage.initStyle(StageStyle.TRANSPARENT);
            MouseFollow in = new MouseFollow(root, loginstage);
            loginstage.setScene(scene);
            loginstage.show();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public void sendbuttonOnAction(ActionEvent event)
    {
        if(IsMail.isEmail(mailboxTextField.getText().toString())&& CheckEmail.checkemail(mailboxTextField.getText().toString())==false)
        {   sendbutton.setDisable(true);
            SecurityCode2 securityCode2=new SecurityCode2(this);
            securityCode2.start();
            mailboxlabel.setText("邮箱有效！");
            String s= RandomStringUtils.randomNumeric(6);
            content=RandomStringUtils.randomAlphabetic(6);
            String address=mailboxTextField.getText().toString();
            boolean b = EmailService.emailservice(content,address);
            System.out.println("发送"+(b?"成功":"失败"));
        }
        else{
            mailboxlabel.setText("邮箱无效！");
        }

    }
    public void test1(int time){
        yanzheng.setText("重新发送还需等待"+"("+time+")"+"秒");
    }
    public void test2()
    {
        yanzheng.setText("点击重新发送验证码");
        sendbutton.setDisable(false);
    }
    public void confirmbuttonOnAction(ActionEvent event)
    {
        CheckSecurityCode csc=new CheckSecurityCode();
        if(csc.check(securitycodeTextField.getText(),mailboxTextField.getText())) {
            User user=new User();
            user.setMailbox(mailboxTextField.getText());
            Local.user=user;
            confirmlabel.setText("验证码正确！");
            Stage stage=(Stage) confirmbutton.getScene().getWindow();
            stage.close();
            createresetForm();
        }
        else confirmlabel.setText("验证码错误！");
    }
    public void createresetForm(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader();
            fxmlLoader.setLocation(getClass().getResource("reset_password.fxml"));
            Parent root = fxmlLoader.load();
            Stage resetstage=new Stage();
            Scene scene = new Scene(root, 600, 500);
            resetstage.initStyle(StageStyle.TRANSPARENT);
            MouseFollow in = new MouseFollow(root, resetstage);
            resetstage.setScene(scene);
            resetstage.show();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}